package com.entersoft.sicep.log4j;

import java.io.File;
import java.util.StringTokenizer;
/**
 * Package: log4j
 * Clase: Fileutils
 * Descripción: Control de valores.
 * Versión SICEP: 3.0
 * Autor: Cesar Pino - Entersoft
 * fecha creación: xx/xx/2012
 * ----------------------------------
 * 
 * Modificado por: XXXXXXXXXXXXXXXXXX
 * Motivo de actualización: XXXXXXXXX
 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 * fecha actualización: xx/xx/2012
 */
public final class Fileutils {
    //private static org.apache.log4j.Category logger = null;
    // Log4jUtil.instance().getCategory( Fileutils.class );	


    private Fileutils(){
            super();
    }

    /**
     * 
     * @param p_pathName
     * @return
     */
    public static File findFirstFileInClasspath(String p_pathName)
    {
            return findFirstFile(p_pathName, System.getProperty("java.class.path"));
    }	


    /**
     * 
     * @param p_pathName
     * @param p_pathList
     * @return
     */
    public static File findFirstFile(String p_pathName, String p_pathList)
    {
            File f = p_pathName == null ? null : new File(p_pathName);
            return findFirstFile(f, p_pathList);
    }



    /**
     * Busca la primera coincidencia de archivo en el CLASSPATH de la current vm
     * @param p_file
     * @return
     */
    public static File findFirstFileInClasspath(File p_file)
    {
            return findFirstFile(p_file, System.getProperty("java.class.path"));
    }

    /**
     * Busca la primera coincidencia de archivo en la lista de path especificado
     * @param p_file
     * @param p_pathList
     * @return
     */	
    public static File findFirstFile(File p_file, String p_pathList)
    {
            File result = null;
            if(p_file == null || p_pathList == null)
                    return null;

            boolean isDirectory = p_file.isDirectory();
            for(StringTokenizer tokenizer = new StringTokenizer(p_pathList, File.pathSeparator); result == null && tokenizer.hasMoreTokens();)
            {
                    String pathName = tokenizer.nextToken();
                    File path = (new File(pathName)).getAbsoluteFile();
                    if(isDirectory)
                    {
                            if(p_file.equals(path))
                                    result = path;
                    } else
                    if(path.isFile())
                    {
                            if(p_file.equals(path))
                                    result = path;
                            else
                            if(path.getParentFile() != null)
                            {
                                    File composedFile = new File(path.getParentFile(), p_file.getPath());
                                    if(composedFile.exists() && composedFile.isFile())
                                            result = composedFile;
                            }
                    } else
                    {
                            File composedFile = new File(path, p_file.getPath());
                            if(composedFile.exists() && composedFile.isFile())
                                    result = composedFile;
                    }
            }


            return result;
    }

}
